package com.hx.collection.shardingmap;

import java.util.*;

/**
 * 描述：屏蔽sharding中返回集合的大小问题
 * push: plan_time id transfer_no
 * message: send_time message_id transfer_no rrid
 *
 * @author Minghua Chen
 * @date 2020-11-07 18:02
 */
public class ShardingAlgorithmTest {

//      private final Map<String, Collection<T>> columnNameAndShardingValuesMap;
//      private final Map<String, Range<T>> columnNameAndRangeValuesMap;

    private static final List<String> pushKey = Arrays.asList("plan_time", "PLAN_TIME", "id", "ID", "transfer_no", "TRANSFER_NO");
    private static final List<String> messageKey = Arrays.asList("send_time", "SEND_TIME", "message_id", "MESSAGE_ID", "TRANSFER_NO", "transfer_no", "rrid", "RRID");
    private static final List<String> availableTargetNames = Arrays.asList("table101", "table102", "table103");

    public static void main(String[] args) {
        // 分片条件
        Map<String, Collection<String>> columnNameAndShardingValuesMap = new HashMap<>();
        ArrayList<String> planList = new ArrayList<>();
        ArrayList<String> idList = new ArrayList<>();
        ArrayList<String> transList = new ArrayList<>();
        columnNameAndShardingValuesMap.put("PLAN_TIME", planList);
        columnNameAndShardingValuesMap.put("id", idList);
        columnNameAndShardingValuesMap.put("transfer_no", transList);

        ShardingAlgorithmTest test = new ShardingAlgorithmTest();
        Collection<String> realTables = test.doSharding(availableTargetNames, columnNameAndShardingValuesMap);
        realTables.forEach(System.out::println);
    }

    /**
     * 返回具体的表名
     *
     * @param availableTargetNames           实际物理表名
     * @param columnNameAndShardingValuesMap 对应的条件
     * @return
     */
    private Collection<String> doSharding(Collection<String> availableTargetNames, Map<String, Collection<String>> columnNameAndShardingValuesMap) {
        // 由于返回的column大小写不确定 所以需要这样操作
        if (columnNameAndShardingValuesMap != null && columnNameAndShardingValuesMap.size() > 0) {
            // if的顺序决定了分片键的优先级 "plan_time" = "PLAN_TIME" >"id" ="ID" >"transfer_no"="TRANSFER_NO"
            if (columnNameAndShardingValuesMap.containsKey(pushKey.get(0))) {
                return processPlanTime(columnNameAndShardingValuesMap, pushKey.get(0));
            }
            if (columnNameAndShardingValuesMap.containsKey(pushKey.get(1))) {
                return processPlanTime(columnNameAndShardingValuesMap, pushKey.get(1));
            }
            if (columnNameAndShardingValuesMap.containsKey(pushKey.get(2))) {
                return processId(columnNameAndShardingValuesMap, pushKey.get(0));
            }
            if (columnNameAndShardingValuesMap.containsKey(pushKey.get(3))) {
                return processId(columnNameAndShardingValuesMap, pushKey.get(3));
            }
            if (columnNameAndShardingValuesMap.containsKey(pushKey.get(4))) {
                return processTransferNo(columnNameAndShardingValuesMap, pushKey.get(4));
            }
            if (columnNameAndShardingValuesMap.containsKey(pushKey.get(5))) {
                return processTransferNo(columnNameAndShardingValuesMap, pushKey.get(5));
            }
        }

        ArrayList<String> realTable = new ArrayList<>();
        realTable.add("table 默认表名");
        return realTable;
    }

    private Collection<String> processTransferNo(Map<String, Collection<String>> columnNameAndShardingValuesMap, String key) {
        Collection<String> strings = columnNameAndShardingValuesMap.get(key);
        // 获取string里面的值 进行处理  返回对应的tableList
        ArrayList<String> realTable = new ArrayList<>();
        realTable.add(availableTargetNames.get(2));
        return realTable;
    }

    private Collection<String> processId(Map<String, Collection<String>> columnNameAndShardingValuesMap, String key) {
        Collection<String> strings = columnNameAndShardingValuesMap.get(key);
        // 获取string里面的值 进行处理  返回对应的tableList
        ArrayList<String> realTable = new ArrayList<>();
        realTable.add(availableTargetNames.get(1));
        return realTable;
    }

    private Collection<String> processPlanTime(Map<String, Collection<String>> columnNameAndShardingValuesMap, String key) {
        Collection<String> strings = columnNameAndShardingValuesMap.get(key);
        // 获取string里面的值 进行处理  返回对应的tableList
        ArrayList<String> realTable = new ArrayList<>();
        realTable.add(availableTargetNames.get(0));
        return realTable;
    }
}
